package com.zcgk.modules.file.controller;


import com.zcgk.core.domain.JsonResult;
import com.zcgk.core.utils.ExcelUtils;
import com.zcgk.modules.file.entity.AttachmentEntity;
import com.zcgk.modules.file.entity.vo.req.AddAttachmentReqVO;
import com.zcgk.modules.file.entity.vo.resp.DownloadAttachmentRespVO;
import com.zcgk.modules.file.service.IAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 附件表前端控制器
 * </p>
 *
 * @author Francis
 * @since 2020-02-20
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(value = "附件管理 -- controller", tags = "附件管理 -- 操作api", description = "附件的上传、下载、预览、删除等接口")
public class AttachmentController {

    @Autowired
    IAttachmentService attachmentService;


    @PostMapping("/add")
    @ApiOperation("【新、老系统共用】文件上传")
    public JsonResult<String> add(MultipartFile file, AddAttachmentReqVO reqVO) {
        attachmentService.add(file, reqVO);

        return JsonResult.success(null);
    }


    @GetMapping("/queryUrlListByResId")
    @ApiOperation("【新系统用】根据 资源id 查询路径集合，一般用于图片预览")
    public JsonResult<List<String>> queryUrlListByResId(@ApiParam("资源id") String resId) {
        List<String> urlList = attachmentService.queryUrlListByResId(resId);

        return JsonResult.success(urlList);
    }


    @GetMapping("/queryUrlListByResId4OldSys")
    @ApiOperation("【老系统用】根据 资源id、系统id、表名 查询路径集合，一般用于图片预览")
    public JsonResult<List<String>> queryUrlListByResId4OldSys(@ApiParam("资源id") String resId,
                                                               @ApiParam("系统id") String systemId,
                                                               @ApiParam("表名") String tableName) {
        List<String> urlList = attachmentService.queryUrlListByResId4OldSys(resId, systemId, tableName);

        return JsonResult.success(urlList);
    }


    @GetMapping("/downloadByResId")
    @ApiOperation("【新系统用】根据 资源id 下载文件。如果是多文件，则下载的是压缩包")
    public ResponseEntity<byte[]> downloadByResId(@ApiParam("资源id") String resId) {
        DownloadAttachmentRespVO respVO = attachmentService.downloadByResId(resId);

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        header.setContentDispositionFormData("attachment", respVO.getAttName());

        return new ResponseEntity<byte[]>(respVO.getData(), header, HttpStatus.OK);
    }


    @GetMapping("/downloadByResId4OldSys")
    @ApiOperation("【老系统用】根据 资源id、系统id、表名 下载文件。如果是多文件，则下载的是压缩包")
    public ResponseEntity<byte[]> downloadByResId4OldSys(@ApiParam("资源id") String resId,
                                                         @ApiParam("系统id") String systemId,
                                                         @ApiParam("表名") String tableName) {
        DownloadAttachmentRespVO respVO = attachmentService.downloadByResId4OldSys(resId, systemId, tableName);

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        header.setContentDispositionFormData("attachment", respVO.getAttName());

        return new ResponseEntity<byte[]>(respVO.getData(), header, HttpStatus.OK);
    }


    @GetMapping("/deleteById")
    @ApiOperation("【新、老系统共用】单文件删除，根据 附件主键id 删除附件")
    public JsonResult<String> deleteById(@ApiParam("附件主键id") String attId) {
        attachmentService.deleteById(attId);

        return JsonResult.success(null);
    }


    @GetMapping("/deleteByResId")
    @ApiOperation("【新系统用】多文件(若有多个)删除，根据 附件资源id 删除附件")
    public JsonResult<String> deleteByResId(@ApiParam("附件资源id") String resId) {
        attachmentService.deleteByResId(resId);

        return JsonResult.success(null);
    }


    @GetMapping("/deleteByResId4OldSys")
    @ApiOperation("【老系统用】多文件(若有多个)删除，根据 附件资源id 、系统id、表名 删除附件")
    public JsonResult<String> deleteByResId4OldSys(@ApiParam("资源id") String resId,
                                                   @ApiParam("系统id") String systemId,
                                                   @ApiParam("表名") String tableName) {
        attachmentService.deleteByResId4OldSys(resId, systemId, tableName);

        return JsonResult.success(null);
    }


    @GetMapping("/exportExcel")
    @ApiOperation("测试 导出excel 工具类")
    public ResponseEntity<byte[]> exportExcel() {
        List<AttachmentEntity> list = attachmentService.list(null);

        DownloadAttachmentRespVO respVO = ExcelUtils.export2Excel(list, AttachmentEntity.class, "测试导出", null);

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        header.setContentDispositionFormData("attachment", respVO.getAttName());

        return new ResponseEntity<>(respVO.getData(), header, HttpStatus.OK);
    }

}

